package com.ipartek.formacion.gestformwww.controlador;

import com.ipartek.formacion.gestformwww.modelo.*;
import com.ipartek.formacion.gestformwww.modelo.beans.*;
import com.ipartek.formacion.gestformwww.modelo.beans.excepciones.GestorFormacionExcepcion;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Xarles & Borja
 * Modificado por Karmele y Alvaro
 */
public class VisualizarSMSAccion implements Accion {

   
    private final String visualizarcurso = "visualizar-curso-accion";
    private final String listarcursos = "listar-cursos-accion";

/**
 * 
 * @param servlet
 * @param request
 * @param response
 * @return
 */
    public AccionRouter ejecuta(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response) {
        HttpSession sesion;
        sesion = request.getSession();
        AccionRouter router=new AccionRouter("principal");
          AccionServlet as=(AccionServlet) servlet;
        Integer idAlumno = -1;
        Integer idCurso = -1;

        ModeloSMS msms = (ModeloSMS) servlet.getServletContext().getAttribute("ModeloSMS");
        ArrayList<SMSEnviosAlumno> sms = new ArrayList<SMSEnviosAlumno>();

        if ((Verificador.numeroValido(request.getParameter("idAlumno")) || (request.getAttribute("idAlumno") != null))&&
                (Verificador.numeroValido(request.getParameter("idCurso")) || (request.getAttribute("idCurso") != null))){
                if (request.getAttribute("idAlumno") != null) {
                    idAlumno = (Integer) request.getAttribute("idAlumno");
                } else {
                    idAlumno = Integer.parseInt(request.getParameter("idAlumno"));
                }
                if (request.getAttribute("idCurso") != null) {
                    idCurso = (Integer) request.getAttribute("idCurso");
                } else {
                    idCurso = Integer.parseInt(request.getParameter("idCurso"));
                }
            try {
                sms = msms.listarSMS(idCurso, idAlumno);
            } catch (GestorFormacionExcepcion ex) {
                Logger.getLogger(VisualizarSMSAccion.class.getName()).log(Level.SEVERE, null, ex);
            }

                if (sms!=null){
                    request.setAttribute("listasms", sms);
                    request.setAttribute("contenido-alumno", "sms-alumno");
                }
                else{
                    if(request.getParameter("idCurso")!=null){
                        request.setAttribute("idCurso",request.getParameter("idCurso"));
                    try {
                        router=as.getFactory().getAction(as.getNombreClaseRecurso(visualizarcurso),servlet.getClass().getClassLoader()).ejecuta(servlet, request, response);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(VisualizarSMSAccion.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (InstantiationException ex) {
                        Logger.getLogger(VisualizarSMSAccion.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IllegalAccessException ex) {
                        Logger.getLogger(VisualizarSMSAccion.class.getName()).log(Level.SEVERE, null, ex);
                    }
                        
                    }else{
                    try {
                        router=as.getFactory().getAction(as.getNombreClaseRecurso(listarcursos),servlet.getClass().getClassLoader()).ejecuta(servlet, request, response);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(VisualizarSMSAccion.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (InstantiationException ex) {
                        Logger.getLogger(VisualizarSMSAccion.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IllegalAccessException ex) {
                        Logger.getLogger(VisualizarSMSAccion.class.getName()).log(Level.SEVERE, null, ex);
                    }
                       
                    }
                 }
        }
        else{
            try {
                router=as.getFactory().getAction(as.getNombreClaseRecurso(listarcursos),servlet.getClass().getClassLoader()).ejecuta(servlet, request, response);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(VisualizarSMSAccion.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(VisualizarSMSAccion.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(VisualizarSMSAccion.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return router;
    }

    public void inicializa(AccionFactory factory) {

    }

}